package com.hns.can.service.service;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.service.entity.BaseDrvChkParamInfoEntity;
import com.hns.can.service.vo.SafetyEvalVo;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.DataUtil;
import com.hns.tool.pub.PubUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 安保考核设置-评分设置Service
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class SafetyEvalService extends CommonService {

    /**
     * @param paraMap
     * @return
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-11-10
     * @功能说明：安保考核参数设置 - 获取该组织机构下的安保考核参数设置信息，如果没有的话显示为默认的信息
     */
    @SuppressWarnings("unchecked")
    @Transactional
    public List<BaseDrvChkParamInfoEntity> searchSafetyEvalInfo(Map<String, Object> paraMap) {
        List<BaseDrvChkParamInfoEntity> list = PubUtil.convertListMapToObject(
                this.sqlMapDao.selectList("baseDrvChkParamInfo.searchBaseDrvChkParamInfo", paraMap), BaseDrvChkParamInfoEntity.class);
        return list;
    }

    /**
     * @param data
     * @throws Exception
     * @author:JeffXu
     * @email:835264047@qq.com
     * @创建日期:2014-11-12
     * @功能说明：安保考核参数设置 - 修改安保考核参数设置信息
     */
    @Transactional
    public void uptSafetyEvalInfo(SafetyEvalVo data) throws Exception {
        String seleId = data.getSeleId();
        String currId = data.getCurrId();
        String corpId = data.getCorpId();
        List<String> chkIds = data.getChkIds();
        List<BigDecimal> accIllWVs = data.getAccIllWVs();
        List<BigDecimal> accScoreWVs = data.getAccScoreWVs();
        List<BigDecimal> accDedPoints = data.getAccDedPoints();
        List<BigDecimal> accScoreACs = data.getAccScoreACs();
        List<BigDecimal> illScoreWVs = data.getIllScoreWVs();
        List<BigDecimal> illDedPoints = data.getIllDedPoints();
        List<BigDecimal> illScoreACs = data.getIllScoreACs();
        SysUserEntity user = this.getCurrentUser().getUser();

        int len1 = accIllWVs.size();
        int len2 = accIllWVs.size()+accScoreWVs.size();
        int len3 = accIllWVs.size()+accScoreWVs.size()+illScoreWVs.size();

        if (seleId.equals(currId)) { // 更新已有的数据
            for(int i = 0; i < chkIds.size(); i++){
                BaseDrvChkParamInfoEntity entity = this.commonEntityDao.find(chkIds.get(i), BaseDrvChkParamInfoEntity.class);
                if(i < len1){
                    entity.setWeightValue(accIllWVs.get(i));
                }else if(i >= len1 && i < len2){
                    entity.setWeightValue(accScoreWVs.get(i-accIllWVs.size()));
                    entity.setDeductValue(accDedPoints.get(i-accIllWVs.size()));
                    entity.setAllowCount(accScoreACs.get(i-accIllWVs.size()));
                }else if(i >= len2 && i < len3){
                    entity.setWeightValue(illScoreWVs.get(i-(accIllWVs.size()+accScoreWVs.size())));
                    entity.setDeductValue(illDedPoints.get(i-(accIllWVs.size()+accScoreWVs.size())));
                    entity.setAllowCount(illScoreACs.get(i-(accIllWVs.size()+accScoreWVs.size())));
                }
                entity.setUpdateTime(new Date());
                entity.setUpdateUser(user.getAcctName());
                this.commonEntityDao.update(entity);
            }
        } else { // 添加新数据
            for(int i = 0; i < chkIds.size(); i++){
                BaseDrvChkParamInfoEntity entity = this.commonEntityDao.find(chkIds.get(i), BaseDrvChkParamInfoEntity.class);
                BaseDrvChkParamInfoEntity dbentity = new BaseDrvChkParamInfoEntity();
                dbentity.setChkId(DataUtil.sequenceID());
                if(i < len1){
                    PubUtil.copyPropertiesInclude(entity, dbentity, new String[]{"organLineType","chkCgyCode","chkCgyName","chkCode","chkName","param","deductValue","allowUnit","allowCount"});
                    dbentity.setWeightValue(accIllWVs.get(i));
                    dbentity.setCorpId(corpId);
                }else if(i >= len1 && i < len2){
                    PubUtil.copyPropertiesInclude(entity, dbentity, new String[]{"organLineType","chkCgyCode","chkCgyName","chkCode","chkName","param","allowUnit"});
                    dbentity.setWeightValue(accScoreWVs.get(i-accIllWVs.size()));
                    dbentity.setDeductValue(accDedPoints.get(i-accIllWVs.size()));
                    dbentity.setAllowCount(accScoreACs.get(i-accIllWVs.size()));
                    dbentity.setCorpId(corpId);
                }else if(i >= len2 && i < len3){
                    PubUtil.copyPropertiesInclude(entity, dbentity, new String[]{"organLineType","chkCgyCode","chkCgyName","chkCode","chkName","param","allowUnit"});
                    dbentity.setWeightValue(illScoreWVs.get(i-(accIllWVs.size()+accScoreWVs.size())));
                    dbentity.setDeductValue(illDedPoints.get(i-(accIllWVs.size()+accScoreWVs.size())));
                    dbentity.setAllowCount(illScoreACs.get(i-(accIllWVs.size()+accScoreWVs.size())));
                    dbentity.setCorpId(corpId);
                }
                dbentity.setOrganLineId(seleId);
                dbentity.setCrtTime(new Date());
                dbentity.setCrtUser(user.getAcctName());
                dbentity.setUpdateTime(new Date());
                dbentity.setUpdateUser(user.getAcctName());
                this.commonEntityDao.save(dbentity);
            }
        }
    }

}
